package com.zmzncs.lmtc.module.park.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.lmtc.common.pojo.PageInfo;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.pojo.bolian.BoLianParkUpdateForm;
import com.zmzncs.lmtc.common.pojo.bolian.BolianQueryParkByIdRes;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.park.pojo.bolian_pull.BolianPull;
import com.zmzncs.lmtc.module.park.pojo.park_info.*;
import com.zmzncs.lmtc.module.park.service.BolianPullService;
import com.zmzncs.lmtc.module.park.service.ParkInfoService;
import io.swagger.annotations.*;
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-09-29
 */
@Api(tags = "停车场信息")
@RestController
@RequestMapping("/park/park-info")
@Validated
public class ParkInfoController {

    @Autowired
    ParkInfoService parkInfoService;
    @Autowired
    BolianPullService bolianPullService;

    @ApiOperation(value = "获取泊链停车场列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @GetMapping(value = "/getParkList")
    public Rest<List<ParkInfoVO>> getParkList(){
        List<ParkInfoDTO> parkInfoDTOList = parkInfoService.getParkList();
        List<ParkInfoVO> parkInfoVOList = JsonUtil.listTransform(parkInfoDTOList, ParkInfoVO.class);

        return RestUtil.success(parkInfoVOList);
    }


    @ApiOperation(value = "获取停车场分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "/getParkPage")
    public Rest<Map<String,Object>> getParkPage(@RequestBody ParkInfoListForm form){

        Map<String,Object> map = new HashMap<>();

        IPage<ParkInfoDTO> parkInfoDTOIPage = parkInfoService.getParkPage(form);
        PageInfo<ParkTableVO> parkTableVOPageInfo = JsonUtil.pageTransform(parkInfoDTOIPage, ParkTableVO.class);
        map.put("ipage",parkTableVOPageInfo);

        List<BolianPull> bolianPulls = bolianPullService.getPendingBolianPark();
        if (bolianPulls != null && bolianPulls.size() > 0){
            map.put("count",bolianPulls.size());
        } else {
            map.put("count",0);
        }
        return RestUtil.success(map);
    }

    @ApiOperation(value = "显示/关闭停车场")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "parkId", value = "停车场id", required = true),
            @ApiImplicitParam(paramType="query", name = "isShow", value = "1：开启，0：关闭", required = true),
    })
    @PostMapping(value = "/switchParkShow")
    public Rest switchParkShow(@NotNull Long parkId, @NotNull Integer isShow){
        ParkInfo parkInfo = new ParkInfo();
        parkInfo.setId(parkId);
        parkInfo.setIsShow(isShow);
        parkInfoService.updateById(parkInfo);

        return RestUtil.success();
    }

    @ApiOperation(value = "根据id获取停车场停车场")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(name = "id", value = "停车场id", required = true),
    })
    @GetMapping(value = "/get-by-id")
    public Rest<ParkInfoVO> getById(@NotNull Long id){
        ParkInfo parkInfo = parkInfoService.getById(id);

        ParkInfoVO parkInfoVO = new ParkInfoVO();
        if (parkInfo != null){
            BeanUtils.copyProperties(parkInfo, parkInfoVO);
        }
        if(parkInfo.getParkImgUrl() != null && !(parkInfo.getParkImgUrl().equals(""))){
            parkInfoVO.setParkImgUrls(parkInfo.getParkImgUrl().split(","));
        } else {
            String[] strings = new String[0];
            parkInfoVO.setParkImgUrls(strings);
        }

        return RestUtil.success(parkInfoVO);
    }

    @ApiOperation(value = "修改停车场")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "/update")
    public Rest update(@RequestBody @Validated ParkInfoUpdateForm form){
        ParkInfo parkInfo = new ParkInfo();
        BeanUtils.copyProperties(form, parkInfo);
        String parkImagUrl = StringUtils.join(form.getParkImgUrls(), ',');
        parkInfo.setParkImgUrl(parkImagUrl);
        parkInfo.setEmptyParkSpace(form.getTotalParkSpace());

        parkInfoService.updateById(parkInfo);

        return RestUtil.success();
    }


    @ApiOperation(value = "修改停车场收费规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(name = "id", value = "停车场id", required = true),
            @ApiImplicitParam(name = "priceDesc", value = "停车场收费规则", required = true),
    })
    @PostMapping(value = "/updatePriceDesc")
    public Rest updatePriceDesc(@NotNull Long id,@NotNull @Size(max = 25) String priceDesc){
        ParkInfo parkInfo = new ParkInfo();
        parkInfo.setId(id);
        parkInfo.setPriceDesc(priceDesc);

        parkInfoService.updateById(parkInfo);

        return RestUtil.success();
    }

    @ApiOperation(value = "获取咪网停车场信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query",name = "parkId", value = "业主编号", required = true),
    })
    @GetMapping(value = "getParkingLotByCode")
    public Rest getParkingLotByCode(@NotNull @Size(max = 25) String parkId) {
        ParkInfoDTO parkInfoDTO = parkInfoService.getParkingLotByCode(parkId);
        return RestUtil.success(parkInfoDTO);
    }

    @ApiOperation(value = "咪网停车场从泊链更新/禁用")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @GetMapping(value = "updateParkToBolian")
    public Rest updateParkToBolian(@Validated BoLianParkUpdateForm form) {
        parkInfoService.updateParkToBolian(form);
        return RestUtil.success();
    }

    @ApiOperation(value = "添加道尔停车场信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),

    })
    @PostMapping("getDaoErParkInfo")
    public Rest getDaoErParkInfo(@RequestBody @Valid DaoerParkAddForm form){
        parkInfoService.getDaoErParkInfo(form);
        return RestUtil.success();
    }

    @ApiOperation(value = "添加腾达停车场信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "/add-tengda-park")
    public Rest addTengdaPark(@RequestBody @Validated TengdaParkAddForm form ){
        parkInfoService.addTengdaPark(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "查询泊链停车场信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name="comId", value="泊链comId", required = true),
    })
    @PostMapping(value = "/getBolianParkInfo")
    public Rest<BolianQueryParkByIdRes> getBolianParkInfo(@NotNull String comId){
        BolianQueryParkByIdRes bolianParkInfo = parkInfoService.getBolianParkInfo(comId);
        return RestUtil.success(bolianParkInfo);
    }


    @ApiOperation(value = "添加停车场信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "/addParkInfo")
    public Rest addParkInfo(@RequestBody ParkInfoAddFrom from ){
      parkInfoService.addParkInfo(from);
      return RestUtil.success();
    }

    @ApiOperation(value = "查询未设置车场主的停车场信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @GetMapping(value = "/no-manager-park")
    public Rest<List<ParkInfo>> noManagerPark(){
        List<ParkInfoDTO> infoDTOList = parkInfoService.noManagerPark();
        List<ParkInfo> parkInfos = JsonUtil.listTransform(infoDTOList, ParkInfo.class);
        return RestUtil.success(parkInfos);
    }

    @ApiOperation(value = "查询未设置预约规则的停车场")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @GetMapping(value = "/get-no-reserve-park")
    public Rest<List<ParkInfoDTO>> getNoReservePark(){
        List<ParkInfoDTO> infoDTOList = parkInfoService.getNoReservePark();
        return RestUtil.success(infoDTOList);
    }

}

